package com.imesne.office.excel.html;

import com.imesne.office.excel.utils.ExcelKitUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liyd on 17/7/18.
 */
public class ExcelHtmlWriterConfig {

    private ExcelHtmlPrinter excelHtmlPrinter;

    private HtmlHelper htmlHelper;

    private ExcelCellHtmlAttrProcessor excelCellHtmlAttrProcessor;

    /**
     * 默认的css样式文件
     */
    private String defaultExcelCss;

    private boolean completeHTML;

    private Map<Integer, CellBound> cellBounds;

    public ExcelHtmlWriterConfig() {
        this.excelHtmlPrinter = new ExcelHtmlPrinterImpl(this);
        this.defaultExcelCss = "excelStyle.css";
        cellBounds = new HashMap<>();
    }


    /**
     * 指定边界 开始单元格和结束单元格
     *
     * @param sheetNum  the sheet num
     * @param firstCell the first cell
     * @param endCell   the end cell
     */
    public void addCellBound(int sheetNum, String firstCell, String endCell) {
        cellBounds.put(sheetNum, new CellBound(firstCell, endCell));
    }

    /**
     * 指定边界，结束单元格，开始单元格默认从1开始
     *
     * @param sheetNum the sheet num
     * @param endCell  the end cell
     */
    public void addCellBound(int sheetNum, String endCell) {
        cellBounds.put(sheetNum, new CellBound("A1", endCell));
    }

    /**
     * 指定边界
     * <pre>
     * key = sheetNum
     * value:
     *  [0] = firstCell
     *  [1] = endCell
     * </pre>
     *
     * @param map
     */
    public void addCellBound(Map<Integer, String[]> map) {
        for (Map.Entry<Integer, String[]> entry : map.entrySet()) {
            String[] value = entry.getValue();
            this.addCellBound(entry.getKey(), value[0], value[1]);
        }
    }


    /**
     * 获取边界
     *
     * @param sheetNum
     * @return
     */
    public CellBound getCellBound(int sheetNum) {
        return cellBounds.get(sheetNum);
    }

    public String getDefaultExcelCss() {
        return defaultExcelCss;
    }

    public void setDefaultExcelCss(String defaultExcelCss) {
        this.defaultExcelCss = defaultExcelCss;
    }

    public boolean isCompleteHTML() {
        return completeHTML;
    }

    public void setCompleteHTML(boolean completeHTML) {
        this.completeHTML = completeHTML;
    }

    public ExcelHtmlPrinter getExcelHtmlPrinter() {
        return excelHtmlPrinter;
    }

    public void setExcelHtmlPrinter(ExcelHtmlPrinter excelHtmlPrinter) {
        this.excelHtmlPrinter = excelHtmlPrinter;
    }

    public HtmlHelper getHtmlHelper() {
        return htmlHelper;
    }

    public void setHtmlHelper(HtmlHelper htmlHelper) {
        this.htmlHelper = htmlHelper;
    }

    public ExcelCellHtmlAttrProcessor getExcelCellHtmlAttrProcessor() {
        return excelCellHtmlAttrProcessor;
    }

    public void setExcelCellHtmlAttrProcessor(ExcelCellHtmlAttrProcessor excelCellHtmlAttrProcessor) {
        this.excelCellHtmlAttrProcessor = excelCellHtmlAttrProcessor;
    }

    public static class CellBound {
        private int firstCellNum;
        private int endCellNum;
        private int firstRowNum;
        private int endRowNum;

        public CellBound(String firstCell, String endCell) {
            int[] first = ExcelKitUtils.cellRefToNum(firstCell);
            int[] end = ExcelKitUtils.cellRefToNum(endCell);
            this.firstCellNum = first[0];
            this.firstRowNum = first[1];
            this.endCellNum = end[0];
            this.endRowNum = end[1];
        }

        public int getFirstCellNum() {
            return firstCellNum;
        }

        public int getEndCellNum() {
            return endCellNum;
        }

        public int getFirstRowNum() {
            return firstRowNum;
        }

        public int getEndRowNum() {
            return endRowNum;
        }
    }


}
